﻿var ns = namespace("kaskelot.sp.ui");

ns.adminblankett = function () {

    function setupFields() {

        var list = $("#faltList").sortable({
            placeholder: "ui-state-highlight"
        });

    	$("#ChildIdList").multiselect({ searchable: false });
    	//$("#ChildIdList").chosen({ allow_single_deselect: true, disable_search: true });
    }

    function getList() {
        $.ajax({
            url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientAdminService.svc/GetBlanketter",
            type: "GET",
            data: {},
            processData: true,
            cache: false,
            success:
                function (data) {
                    if (!data.GetBlanketterResult.HasError) {
                        $("#blankett-results tbody").html(
							$("#blankett-template").render(data.GetBlanketterResult.Data)
						);
                    }
                },
            error:
                function (xhr) {
                    kaskelot.sp.ui.prompts.showErrorPrompt(xhr.responseText);
                }
        });
    }

    function showCreateForm() {
        var selector = "#blankett-form";
        kaskelot.sp.ui.formutils.getAndShowForm({
            params: {
                formName: "BlankettForm",
                idString: "",
                parentIdString: ""
            },
            selector: selector,
            openFunction: function () {
                $("#blankett-form").dialog({ title: "Skapa blankett" });
                setupFields();
            },
            okFunction: function () {
                $(selector).parent().block();

                var prog = kaskelot.sp.ui.formutils.getObject(selector);
                var data = "{\"form\":" + JSON.stringify(prog) + "}";

                var method = "CreateBlankett";

                $.ajax({
                    url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientAdminService.svc/" + method,
                    data: data,
                    type: "POST",
                    processData: false,
                    contentType: "application/json",
                    dataType: "json",
                    success: function (resp) {
                        if (resp[method + "Result"].HasError) {
                            kaskelot.sp.ui.formutils.showError(selector, resp[method + "Result"].ErrorMessage);
                        }
                        else {
                            $(selector).dialog("close");
                            getList();
                        }
                    },
                    error: function (xhr) {
                        kaskelot.sp.ui.formutils.showError(selector, xhr.responseText);
                    }
                });
            }
        });
    }

    function showEditForm(id) {
        var selector = "#blankett-form";
        kaskelot.sp.ui.formutils.getAndShowForm({
            params: {
                formName: "BlankettForm",
                idString: id,
                parentIdString: ""
            },
            selector: selector,
            openFunction: function () {
                $(selector).dialog({ title: "Redigera blankett" });
                setupFields();
            },
            okFunction: function () {
                $(selector).parent().block();

                var prog = kaskelot.sp.ui.formutils.getObject(selector);
                var data = "{\"form\":" + JSON.stringify(prog) + "}";

                var method = "UpdateBlankett";

                $.ajax({
                    url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientAdminService.svc/" + method,
                    data: data,
                    type: "POST",
                    processData: false,
                    contentType: "application/json",
                    dataType: "json",
                    success: function (resp) {
                        if (resp[method + "Result"].HasError) {
                            kaskelot.sp.ui.formutils.showError(selector, resp[method + "Result"].ErrorMessage);
                        }
                        else {
                            $(selector).dialog("close");
                            getList();
                        }
                    },
                    error: function (xhr) {
                        kaskelot.sp.ui.formutils.showError(selector, xhr.responseText);
                    }
                });
            }
        });
    }

    function showDeleteForm(id) {
        var selector = "#blankett-form";
        kaskelot.sp.ui.formutils.getAndShowForm({
            params: {
                formName: "BlankettForm",
                idString: id,
                parentIdString: ""
            },
            selector: selector,
            openFunction: function () {
                $("#blankett-form").dialog({ title: "Ta bort blankett" });
                $("#blankett-form #Namn").attr("disabled", "disabled");
                $("#blankett-form #ChildIdList").attr("disabled", "disabled");
                $("#blankett-form #BlankettTyp").attr("disabled", "disabled");

                setupFields();
            },
            okFunction: function () {
                $(selector).parent().block();

                var prog = kaskelot.sp.ui.formutils.getObject(selector);
                var data = "{\"form\":" + JSON.stringify(prog) + "}";

                var method = "DeleteBlankett";

                $.ajax({
                    url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientAdminService.svc/" + method,
                    data: data,
                    type: "POST",
                    processData: false,
                    contentType: "application/json",
                    dataType: "json",
                    success: function (resp) {
                        if (resp[method + "Result"].HasError) {
                            kaskelot.sp.ui.formutils.showError(selector, resp[method + "Result"].ErrorMessage);
                        }
                        else {
                            $(selector).dialog("close");
                            getList();
                        }
                    },
                    error: function (xhr) {
                        kaskelot.sp.ui.formutils.showError(selector, xhr.responseText);
                    }
                });
            }
        });
    }

    return {
        getList: getList,
        showEditForm: showEditForm,
        showCreateForm: showCreateForm,
        showDeleteForm: showDeleteForm
    };
}();